package com.linktrip.devicemanager.control.sms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.linktrip.devicemanager.entity.SMSLog;
import com.linktrip.devicemanager.server.SMSService;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.util.PageInfo;
import com.linktrip.devicemanager.util.ToolKits;

@Controller
@RequestMapping(value = "/service/sms")
public class SMSControl {
	Logger log = LoggerFactory.getLogger(SMSControl.class);
	@Autowired
	private SMSService server;

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/listSMSLogPaging.lt")
	@ResponseBody
	public String listSMSLogPaging(HttpServletRequest request, HttpServletResponse response) {

		int pageNo = ToolKits.getRequestParamValueForInt(request, "page_no");
		int pageSize = ToolKits.getRequestParamValueForInt(request, "page_size");
		String target = ToolKits.getRequestParamValue(request, "phone_number");
		String time1 = ToolKits.getRequestParamValue(request, "time1");
		String time2 = ToolKits.getRequestParamValue(request, "time2");
		int reply = ToolKits.getRequestParamValueForInt(request, "reply");
		long createTime1 = time1 == null ? -1 : ToolKits.date2Long(time1);
		long createTime2 = time2 == null ? -1 : ToolKits.date2Long(time2);
		if (pageNo <= 0)
			pageNo = 1;
		if (pageSize <= 0)
			pageSize = Constant.PAGE_SIZE;

		int start = (pageNo - 1) * pageSize;
		int end = pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("target", target);
		map.put("createTime1", createTime1);
		map.put("createTime2", createTime2);
		map.put("start", start);
		map.put("end", end);
		map.put("reply", reply);
		List<SMSLog> list = server.paging(map);
		PageInfo<List> plist = new PageInfo<List>(server.totals(map), list);
		plist.setPageNo(pageNo);
		plist.setPageSize(pageSize);
		plist.setPagesSize(plist.pagesSize());

		Gson gson = new Gson();
		return gson.toJson(plist);
	}

	@RequestMapping(value = "/sendMsg.lt")
	@ResponseBody
	public String sendMsg(HttpServletRequest request, HttpServletResponse response) {

		String[] numbers = ToolKits.getRequestParamValue(request, "phonenumber").split(",");
		String content = ToolKits.getRequestParamValue(request, "command");
		boolean reply = server.sendMsg(content, numbers);
		Gson gson = new Gson();
		int res = reply ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		return gson.toJson(res);
	}
	
	@RequestMapping(value = "/batchSend.lt")
	@ResponseBody
	public String batchSendMsg(HttpServletRequest request, HttpServletResponse response) {

		String[] numbers = ToolKits.getRequestParamValue(request, "phonenumber").split(",");
		String content = ToolKits.getRequestParamValue(request, "command");
		boolean reply = server.batchSendMsg(content, numbers);
		Gson gson = new Gson();
		int res = reply ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		return gson.toJson(res);
	}
}
